package com.coocaa.ops.admin.v1.globalparams.biz;

import com.coocaa.ops.admin.v1.globalparams.mapper.IGlobalParamsMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.globalparams.entity.GlobalParamsEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : hql
 * @date : 2020/7/15 17:59
 * @description
 */
@Service
public class GlobalParamsBizImpl extends BaseBizImpl<IGlobalParamsMapper, GlobalParamsEntity> implements IGlobalParamsBiz {

    @Override
    public GlobalParamsEntity addGlobalParams(GlobalParamsEntity entity) {
        getMapper().insert(entity);
        return entity;
    }

    @Override
    public void updateGlobalParams(GlobalParamsEntity entity) {
        GlobalParamsEntity dbEntity = getMapper().selectById(entity.getId());
        if (dbEntity != null && dbEntity.equals(entity)) {
            return;
        }
        getMapper().updateById(entity);
    }

    @Override
    public List<GlobalParamsEntity> getGlobalParamsList(Map params) {
        return getMapper().getGlobalParamsList(params);
    }

    @Override
    public Integer getUsableParamsNum() {
        QueryTemplate<GlobalParamsEntity> query = new QueryTemplate<>();
        query.eq("switch_status", 1)
                .eq("flag", 1);
        return getMapper().selectCount(query);
    }

    @Override
    public boolean isRemarkExist(String remark, Integer id) {
        QueryTemplate<GlobalParamsEntity> query = new QueryTemplate<>();
        query.eq("remark", remark)
                .eq("flag", 1);
        if (id != null) {
            query.ne("id", id);
        }
        return getMapper().selectCount(query) > 0;
    }

}
